<?php

  if (isset($_POST['submit0'])) { // If password entered...
    $word = $_POST['password']; // Variable set as entered password
    setcookie('textadmin', $word, time() + 31536000); // Cookie is set
  } else { // If password form filled in previously...
    if (isset($_COOKIE['textadmin'])) { // Check for cookie
      $word = $_COOKIE['textadmin']; // Variable set as cookie
    }
  }

?>

<?php $thisPage = "create"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Create a new page or edit an existing page</title>
<meta name="robots" content="noindex,nofollow" />
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>
<body>

<div id="wrap">

<?php

// Start password routine
  include_once('password.php');
  if ($word != $le_password) {

?>

<!-- password form //-->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<b>Enter password:</b>
<br />
<input type="password" name="password" size="25" maxlength="25" />
<br />
<input type="submit" name="submit0" value="Submit" />
</p>
</form>
<!-- password form_eof //-->

<?php

    // Routine to display password entry error message.
    if (isset($_POST['submit0'])) {
      if ($_POST['password'] != $le_password) {
        echo '<p">Incorrect password!</p>';
      }
    }

  } else {
// End password routine

?>

<?php include_once('navigation.php'); ?>

<?php

  // New page
  if (isset($_POST['submit1'])) { // Handle the form

    // Check for a file title
    if (strlen($_POST['create']) < 1) {
      $problem = true;
      $response1 = "You didn't enter a page name.<br />";
    }

    // Check for a page title
    if (strlen($_POST['pagetitle']) < 1) {
      $problem = true;
      $response2 = "You didn't enter a page title.<br />";
    }

    if (!$problem) {

      // Create the file title
      $filetitle = $_POST['create'];
      // Remove hyphens from file title (for $pagecontent vars)
      $cleanfiletitle = str_replace("-", "", $filetitle);

      // Create the page title
      $pagetitle = $_POST['pagetitle'];
      $pagetitle = stripslashes($pagetitle);

      // Create the meta description
      $description = $_POST['description'];
      $description = stripslashes($description);

      // Create the meta keywords
      $keywords = $_POST['keywords'];
      $keywords = stripslashes($keywords);

      // Create filetitle.txt file
      $textfilename = $filetitle . '.txt';
      if (file_exists("../textfiles/$textfilename")) {
        $response1 = "Sorry, this page name already exists. Try another.";
      } else {
        touch("../textfiles/$textfilename");

        // Create filetitle.php file
        $phpfilename = $filetitle . '.php';
        touch("../$phpfilename");

        // Define the initial content of filetitle.php (as class template)
        $pagecontent = '<?php include (\'classes/page.class1.php\'); include (\'functions/le-format.php\'); include (\'functions/convert-characters.php\'); $' . $cleanfiletitle . ' = new Page; $filename = "textfiles/' . $textfilename . '"; if (file_exists($filename)) { $file_contents = file_get_contents($filename); $file_contents = autop($file_contents, 0); $file_contents = convert_chars($file_contents, 0); $output = $file_contents; } else { $output = "<p>No content. The associated text file could not be found.</p>"; } $' . $cleanfiletitle . '->Title = "' . $pagetitle . '"; $' . $cleanfiletitle . '->MetaDescription = "' . $description . '"; $' . $cleanfiletitle . '->MetaKeywords = "' . $keywords . '"; $' . $cleanfiletitle . '->ThisPage = "' . $filetitle . '"; $' . $cleanfiletitle . '->SetContent($output); $' . $cleanfiletitle . '->Template(); ?>';

        // Path to the .php file (to write the template)
        $filename = "../$phpfilename";

        // Write the contents
        $fp = fopen($filename, "w") or die("Couldn't open $filename.");
        fwrite($fp, $pagecontent);
        fclose($fp);

        // Feedback
        $response1 = "The files <a href=\"/$filetitle\">$phpfilename</a> and <a href=\"../textfiles/$textfilename\">$textfilename</a> were created.<br />Select <b>Add to Menu</b> above to add the page to the main menu.";
      }
    }
  }

  // Get existing page details
  if (isset($_POST['submit2'])) { // Handle the form

    // Identify the file title
    $filetitle = $_POST['create'];

    // Does the php file exist?
    $phpfilename = $filetitle . '.php';
    if (!file_exists("../$phpfilename")) {
      $response1 = "Sorry, this page name does not exist. Try another.";
    } else {
      $file_contents = file_get_contents("../$phpfilename");
      $string = $file_contents;

      // NOTE! Need to clean $filetitle to avoid any
      // substrings existing in page code (eg "php")
      $filetitle2 = trim($filetitle);

      // Get the title
      // Define start string to find
      $find1 = "->Title";
      // Define finish string to find
      $find2 = $filetitle2 . '->MetaDescription';
      // Start position of start string
      $pos1 = strpos($string, $find1);
      // Start position of finish string
      $pos2 = strpos($string, $find2);
      // Adjust pos1 forward from start pos
      $pos1 = ($pos1 + 11);
      // Adjust pos2 backward from finish pos
      $pos2 = ($pos2 - 4);
      // Set length of title
      $titleLength = ($pos2 - $pos1);
      // Get a substring of length $titleLength from $pos1
      $title = substr($string, $pos1, $titleLength);

      // Get the meta description
      $find3 = "->MetaDescription";
      $find4 = $filetitle2 . '->MetaKeywords';
      $pos3 = strpos($string, $find3);
      $pos4 = strpos($string, $find4);
      $pos3 = ($pos3 + 21);
      $pos4 = ($pos4 - 4);
      $metaDescriptionLength = ($pos4 - $pos3);
      $metaDescription = substr($string, $pos3, $metaDescriptionLength);

      // Get the meta keywords
      $find5 = "->MetaKeywords";
      $find6 = $filetitle2 . '->ThisPage';
      $pos5 = strpos($string, $find5);
      $pos6 = strpos($string, $find6);
      $pos5 = ($pos5 + 18);
      $pos6 = ($pos6 - 4);
      $metaKeywordsLength = ($pos6 - $pos5);
      $metaKeywords = substr($string, $pos5, $metaKeywordsLength);

    }
  }

  // Amend existing page
  if (isset($_POST['submit3'])) { // Handle the form

    // Identify the file title
    $filetitle = $_POST['create'];

    // New page title
    $pagetitle = $_POST['pagetitle'];
    $pagetitle = stripslashes($pagetitle);

    // New meta description
    $description = $_POST['description'];
    $description = stripslashes($description);

    // New meta keywords
    $keywords = $_POST['keywords'];
    $keywords = stripslashes($keywords);

    // Does the php file exist?
    $phpfilename = $filetitle . '.php';
    if (!file_exists("../$phpfilename")) {
      $response1 = "Sorry, this page name does not exist. Try another.";
    } else {

      // Identify filetitle.txt file
      $textfilename = $filetitle . '.txt';
      // Overwrite the content of filetitle.php (as class template)
      $pagecontent = '<?php include (\'classes/page.class1.php\'); include (\'functions/le-format.php\'); include (\'functions/convert-characters.php\'); $' . $filetitle . ' = new Page; $filename = "textfiles/' . $textfilename . '"; if (file_exists($filename)) { $file_contents = file_get_contents($filename); $file_contents = autop($file_contents, 0); $file_contents = convert_chars($file_contents, 0); $output = $file_contents; } else { $output = "<p>No content. The associated text file could not be found.</p>"; } $' . $filetitle . '->Title = "' . $pagetitle . '"; $' . $filetitle . '->MetaDescription = "' . $description . '"; $' . $filetitle . '->MetaKeywords = "' . $keywords . '"; $' . $filetitle . '->ThisPage = "' . $filetitle . '"; $' . $filetitle . '->SetContent($output); $' . $filetitle . '->Template(); ?>';

      // Path to the .php file (to overwrite the template)
      $filename = "../$phpfilename";

      // Overwrite the contents
      $fp = fopen($filename, "w") or die("Couldn't open $filename.");
      fwrite($fp, $pagecontent);
      fclose($fp);

      // Feedback
      $response1 = "The page title and/or meta description and/or keywords for <a href=\"/$filetitle\">$phpfilename</a><br />was/were successfully overwritten.";
    }
  }

?>

<h3>Create a new page</h3>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Page name (no capitals or file suffix)<br />
<input type="text" name="create" id="create" size="40" value="<?php if ((isset($_POST['submit1'])) || (isset($_POST['submit3']))) { echo stripslashes($_POST['create']); } else { echo $create; } ?>" maxlength="60" /><br />
Page title (for "Title" tag)<br />
<input type="text" name="pagetitle" id="pagetitle" size="40" value="<?php if ((isset($_POST['submit1'])) || (isset($_POST['submit3']))) { echo stripslashes($_POST['pagetitle']); } else if (isset($_POST['submit2'])) { echo $title; } else { echo $pagetitle; } ?>" maxlength="60" /><br />
Meta description<br />
<textarea class="textarea" name="description" cols="70" rows="5"><?php if ((isset($_POST['submit1'])) || (isset($_POST['submit3']))) { echo stripslashes($_POST['description']); } else if (isset($_POST['submit2'])) { echo $metaDescription; } else { echo $description; } ?></textarea><br />
Meta keywords<br />
<textarea class="textarea" name="keywords" cols="70" rows="3"><?php if ((isset($_POST['submit1'])) || (isset($_POST['submit3']))) { echo stripslashes($_POST['keywords']); } else if (isset($_POST['submit2'])) { echo $metaKeywords; } else { echo $keywords; } ?></textarea><br />
<input type="submit" name="submit1" value="Create page"> <input type="submit" name="submit2" value="Get page info" /> <input type="submit" name="submit3" value="Overwrite existing" />
</form>

<?php echo $response1 . $response2; ?>

<?php include_once('list-all.php'); ?>

<?php

  } // Required for password routine

?>

<div style="clear: both;"></div>
</div>

</body>
</html>